package com.ruoyi.hospital.utils.doc;

import com.ruoyi.common.core.domain.entity.DocInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DocLoginUser {

    // 令牌自定义标识
    @Value("${token.header}")
    private String header;

    // 令牌秘钥
    @Value("${token.secret}")
    private String secret;

    // 令牌有效期（默认30分钟）
    @Value("${token.expireTime}")
    private int expireTime;

    protected static final long MILLIS_SECOND = 1000;

    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;

    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private HttpServletRequest request;

    /*
    * 获得登陆信息
    * */
     public DocInfo getLoginUser(){
         try {
             return redisCache.getCacheObject(request.getHeader("token"));
         } catch (Exception e) {
             return null;
         }
     }

    /**
     * 设置用户身份信息
     */
    public void setLoginUser(DocInfo loginUser,String userKey)
    {
        if (StringUtils.isNotNull(loginUser))
        {
            //loginUser.setLoginTime(System.currentTimeMillis());
            loginUser.setExpireTime(expireTime * MILLIS_MINUTE);
            // 根据uuid将loginUser缓存
            redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
        }
    }


    /**
     * 删除用户身份信息
     */
    public void delLoginUser(String token)
    {
        if (StringUtils.isNotEmpty(token))
        {
            redisCache.deleteObject(token);
        }
    }
}
